package com.weifly.egp.service.monitor;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;

/**
 * 股票监听服务实现
 *
 * Created by weijianxin on 2018/10/31.
 */
public class StockMonitorServiceImpl implements StockMonitorService {

    private List<StockInfo> stockList = new ArrayList<>();

    private PropertyChangeSupport stockListChangeSupport = new PropertyChangeSupport(this); // 股票列表变更事件

    public void setStockList(List<StockInfo> stockList){
        this.stockList.clear();
        if(stockList!=null){
            this.stockList.addAll(stockList);
        }
        this.stockListChangeSupport.firePropertyChange(EVENT_stockList, null, this.stockList);
    }

    public List<StockInfo> getStockList() {
        return this.stockList;
    }

    public List<StockInfo> getAllStock() {
        List<StockInfo> allStocks = new ArrayList<StockInfo>();
        allStocks.addAll(this.stockList);
        return allStocks;
    }

    public void addStockListListener(PropertyChangeListener listener) {
        this.stockListChangeSupport.addPropertyChangeListener(EVENT_stockList, listener);
    }

    public void fireStockListChange() {
        this.stockListChangeSupport.firePropertyChange(EVENT_stockList, null, this.stockList);
    }

    public StockInfo getStockByIndex(int rowIndex){
        if(rowIndex>=this.stockList.size()){
            return null;
        }
        return this.stockList.get(rowIndex);
    }
}
